package net.mysoft.budgetsoft.system.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import net.mysoft.budgetsoft.system.dao.FlowDao;
import net.mysoft.budgetsoft.system.domain.TFlow;
import net.mysoft.budgetsoft.system.domain.TFlowTaskNode;
import net.mysoft.budgetsoft.system.domain.TFlowTemplate;
import net.mysoft.budgetsoft.system.pagemodel.Flow;
import net.mysoft.budgetsoft.system.service.FlowService;
import net.mysoft.framework.model.DataGrid;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

/**   
 *  
 * @author dagongPGP v1.0 
 * @link <a href=http://www.dagongsoft.net>北京大公信软科技有限公司</a> 
 *  
 * @version $Revision: 1.00  $Date: 2013-07-08 17:25:41 
 */
@Service("flowService")
public class FlowServiceImpl implements FlowService{
	//@Resource
	private FlowDao flowDao;
	
	public FlowDao getFlowDao(){
		return this.flowDao;
	}
	
	public void setFlowDao(FlowDao flowDao){
		this.flowDao = flowDao;
	}
	

	
	@Override
	public void add(Flow flow){
		TFlow t = new TFlow();
		BeanUtils.copyProperties(flow, t);
		flowDao.save(t);
	}
	
	@Override
	public void edit(Flow flow){
		TFlow t = new TFlow();
		BeanUtils.copyProperties(flow, t);
		flowDao.update(t);
	}
	
	@Override
	public void delete(Flow flow) {
		for (String id : flow.getIds().split(",")) {
			Object t =  flowDao.get(TFlow.class, id);
			if (t != null) {
				 flowDao.delete(t);
			}
		}
	}
	
	@Override
	public DataGrid datagrid(Flow flow){
		DataGrid dg = new DataGrid();
		String hql = "from TFlow t where 1=1 ";
		List params = new ArrayList();
		hql = addWhere(flow, hql, params);
		hql = addOrder(flow, hql);
		String totalHql = "select count(*) " + hql;
		List<TFlow> toList = flowDao.find(hql, params, flow.getPage(), flow.getRows());
		List<Flow> oList = new ArrayList<Flow>();
		oList = changeModel(toList);
		dg.setTotal(flowDao.count(totalHql, params));
		dg.setRows(oList);
		return dg;
	}
	
	private String addWhere(Flow flow, String hql, List<Object> params) {
		return hql;
	}
	
	private String addOrder(Flow flow, String hql) {
		if (flow.getSort() != null) {
			hql += " order by " + flow.getSort() + " " + flow.getOrder();
		}
		return hql;
	}
	
	public List<Flow> changeModel(List<TFlow> tFlow ){
		List<Flow> list = new ArrayList<Flow>();
		for(TFlow to:tFlow){
			Flow o = new Flow();
			BeanUtils.copyProperties(to, o);
			list.add(o);
		}
		return list;
	}

	@Override
	public void tianJiaZSJ(Flow flow) {
	
		/*List<TFlowTemplate_YeWuZL> listtt = flowDao.find("from TFlowTemplate_YeWuZL fy where fy.yeWuZLBH = ?",new String[]{"YWZL200707010004"});
		for(TFlowTemplate_YeWuZL fy:listtt){
			TFlowTemplate tft = (TFlowTemplate) flowDao.get(TFlowTemplate.class, fy.getFtl_ID());
			if(tft.getFtc_ID().equals("1")&&tft.getFtl_Valid()==1){
				flowDao.create(tft,null,null,"YongHu2013090600001"); //创建流程
				break;
			}
		}*/
		
		
	}

	@Override
	public Flow findFlowByFtn_ID(Flow flow) {
		TFlowTaskNode t = (TFlowTaskNode) flowDao.get(TFlowTaskNode.class, flow.getFtn_ID());
		TFlowTemplate ft = t.getFlowTemplateNode().getFlowTemplate();
		TFlow tflow = t.getFlow();
		Flow f = new Flow();
		BeanUtils.copyProperties(tflow, f);
		f.setFlo_TemplateID(ft.getFtl_ID());
		if(t!=null)return f;
		return null;
	}


	/**
	 * 流程控制
	 */
	public void flowControl(String templateNode_ID,String xiangMuJBXXBH,String renWuJBXXBH){
		/*flowDao.flowControl(templateNode_ID, xiangMuJBXXBH, renWuJBXXBH);*/
	}
}
